Lesson 6 - newbie exercise
For each scenario write code to solve it.
Scenario - Count the number of words in a sentance.
def wordCount(text)
words = text.split()
count = 0
for i in words:
count += 1
return count
Toggle answer
Scenario - Throw a coin 50 times and count the number of times heads and tails comes up.
import random
headCount = 0
tailCount = 0
for i in range(0,50):
# 0 is head, 1 is tails
coinToss = random.randint(0,1)
if coinToss == 1: headCount += 1
else: tailCount += 1
print "head count", headCount
print "tail count", tailCount
Toggle answer
Scenario - Calculate the speed of a object (speed = distance / time)
def speed(distance, time)
return distance / time
print speed(10, 2)
Toggle answer